package com.cards_game.simple_ddz.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.stereotype.Service;

import com.cards_game.simple_ddz.bean.BaseCardBean;
import com.cards_game.simple_ddz.bean.PlayerBean;
import com.cards_game.simple_ddz.bean.ResultBean;
import com.cards_game.simple_ddz.bean.SimpleDdzBean;
import com.cards_game.simple_ddz.common.AllPrimaryContainer;
import com.cards_game.simple_ddz.common.GameProgressEnum;
import com.cards_game.simple_ddz.common.RoomBean;
import com.cards_game.simple_ddz.component.SimpleDdzCardsUtils;
import com.cards_game.simple_ddz.service.PlayGameService;
import com.cards_game.simple_ddz.verify.SimpleDdzVerify;

/**
 * @author 郭国辉
 * 
 *         2017年6月22日下午6:32:52
 */
@Service
public class PlayGameServiceImpl implements PlayGameService {
	private volatile ConcurrentHashMap<Integer, RoomBean> allRoomMap = AllPrimaryContainer.getAllPrimaryContainer().getAllRoomMap();
	private volatile ConcurrentHashMap<Integer, RoomBean> playerRoomMap = AllPrimaryContainer.getAllPrimaryContainer().getPlayerRoomMap();

	/**
	 * @param cbs
	 * @return
	 */
	@Override
	public boolean discardCards(List<BaseCardBean> cbs) {
		return SimpleDdzVerify.getSimpleDdzVerify().discardCards(cbs);
	}

	/**
	 * @return
	 */
	@Override
	public ResultBean<?> getNowCondition(int userId) {
		RoomBean rb = AllPrimaryContainer.getAllPrimaryContainer().getPlayerRoomMap().get(userId);
		int num = getReadyNum(rb.getId());
		ResultBean<?> res = new ResultBean<>();
		switch (num) {
			case 3:
				return res;
			case 2:
				return res;
			case 1:
				res.setResponseCode(GameProgressEnum.ONE_READY.getValue());
				return res;
			default:
				res.setResponseCode(GameProgressEnum.NONE_READY.getValue());
				return res;
		}
	}

	private int getReadyNum(int roomId) {

		int res = 0;
		Vector<PlayerBean> vec = allRoomMap.get(roomId).getPlayerList();
		if (vec != null && vec.size() != 0) {
			for (PlayerBean pb : vec) {
				if (pb.isReady()) {
					res++;
				}
			}
		}
		return res;
	}

	@Override
	public List<SimpleDdzBean> getSimpleDdzCards(int userId) {
		RoomBean rb = playerRoomMap.get(userId);
		PlayerBean pb = SimpleDdzCardsUtils.cardsToPlayers(userId, rb);
		return pb.getCards();
	}


	/**
	 * 获取底牌
	 * 
	 * @param userId
	 * @return
	 */
	@Override
	public List<SimpleDdzBean> getBottomCards(int userId) {
		RoomBean rb = playerRoomMap.get(userId);
		List<SimpleDdzBean> list = rb.getOriginalCards();
		List<SimpleDdzBean> bottomCards = new ArrayList<>(3);
		for (int i = list.size() - 3; i < list.size(); i++) {
			bottomCards.add(list.get(i));
		}
		return bottomCards;
	}

	private int getNextId(int i, int size) {
		i++;
		if (i == size)
			return 0;
		return i;
	}

	private int getPriorId(int i, int size) {
		i--;
		if (i == -1)
			return size;
		return i;
	}

	/**
	 * @param userId
	 * @return
	 */
	@Override
	public Map<Integer, Integer> getPartner(int userId) {
		RoomBean rb = playerRoomMap.get(userId);
		Vector<PlayerBean> players = rb.getPlayerList();
		Map<Integer, Integer> map = new HashMap<>();

		int self = -1;
		for (int i = 0; i < players.size(); i++) {
			if (players.get(i).getId() == userId) {
				self = i;
				break;
			}
		}
		map.put(-1, players.get(getPriorId(self, players.size())).getCards().size());
		map.put(1, players.get(getNextId(self, players.size())).getCards().size());
		return map;
	}

}
